<?PHP	//header
include('config/config.php');
include('php/header.php');
require "php/functions.php";
?>        

<?PHP	//Connect to database
	include('config/config.php');
	$conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
		or die ('Error Cannot Connect to MySQL');
	@mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
?>

 <?PHP	//pagination; for displaying main window's contain 
		
		$tbl_name=$cfg["tbl"]["inventory"];	//your table name
		/* Setup vars for query. */
		// How many adjacent pages should be shown on each side?
		$adjacents = 3;
		$targetpage = "searchProductName.php"; 			//your file name  (the name of this file)
		$limit = 10; 							//how many items to show per page
		if (isset($_GET['page'])) $page = $_GET['page'];
		else $page = 1;
		if ($page) $start = ($page - 1) * $limit; 			//first item to display on this page
		else $start = 0;									//if no page var is given, set start to 0
		
		/* Get data. */
		$column_name = db_escape_string($cfg["tbl_inventory"]["product_name"]);
		session_start();
		if (isset($_GET['prodName'])) {
			$_SESSION['searchStr'] = $_GET['prodName'];
			$str=db_escape_string($_GET['prodName']);
		}
		else {
			$str=db_escape_string($_SESSION['searchStr']);
		}
		$sql = "SELECT * FROM $tbl_name 
				WHERE $column_name LIKE \"%$str%\"
				LIMIT $start, $limit";
		$result = mysql_query($sql) or die(mysql_error());
		/* 
		   First get total number of rows in data table. 
		   If you have a WHERE clause in your query, make sure you mirror it here.
		*/

		$query = "SELECT COUNT(*) as num FROM (
						SELECT * FROM $tbl_name
						WHERE $column_name LIKE \"%$str%\") AS alias;";
		$total_items = mysql_fetch_array(mysql_query($query));
		$total_items = $total_items['num'];
		
		include('config/pagination.php');
?>
<h2><?PHP echo "$total_items" ?> Search Result for Keyword <?PHP echo "\"$str\"" ?></h2> 
                    
<table id="rounded-corner" summary="Inventory Database">
    <thead>
    	<tr>
        	<th scope="col" class="rounded-company"></th>
            <th scope="col" class="rounded">Barcode</th>
            <th scope="col" class="rounded">Product Name</th>
            <th scope="col" class="rounded">Manufacturer</th>
            <th scope="col" class="rounded">Category</th>
			<th scope="col" class="rounded">Current Stock</th>
			<th scope="col" class="rounded">Current Price</th>
        </tr>
    </thead>
        <tfoot>
    	<tr>
        	<td colspan="6" class="rounded-foot-left"><em>Retrieved from Inventory_xxxxyyyy.txt date DD/MM/YY time HH/MM/SS</em></td>
        	<td class="rounded-foot-right">&nbsp;</td>

        </tr>
    </tfoot>
    <tbody>
	<?PHP	//output from database to main window's box
	echo '<div class="divOuter">';		//parent div ----- just to format multiple children divs inline
	
		echo '<form method="POST" action="buttonRedirect.php">';	
			while($row = mysql_fetch_array($result)){
				echo "<tr>";
					echo "<td><input type=\"checkbox\" name=\"checkbox_Inventory[]\" value='".$row[$cfg["tbl_inventory"]["barcode"]]."'/></td>";
					echo "<td>".$row[$cfg["tbl_inventory"]["barcode"]]."</td>";
					echo "<td>".$row[$cfg["tbl_inventory"]["product_name"]]."</td>";
					echo "<td>".$row[$cfg["tbl_inventory"]['manufacturer']]."</td>";
					echo "<td>".$row[$cfg["tbl_inventory"]['category']]."</td>";
					echo "<td>".$row[$cfg["tbl_inventory"]['local_stock']]."</td>";
					echo "<td>".$row[$cfg["tbl_inventory"]['price']]."</td>";
				echo "</tr>";
			}
	echo '</div>';
	?>
	</tbody>
</table>
	<?PHP
	echo '<div class="divOuter">';	
			//buttons		
			echo '<div id="deleteForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="Delete items"/>'; 
			echo '</div>';

			echo '<div id="addForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="Update item"/>';
			echo '</div>';  
			
			echo '<div id="viewCatForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="View items from category"/>';
			echo '</div>';  
			
		echo '</form>'; 
	echo '</div>';
	?>
     
	<div class="pagination">
	<?PHP	
		echo $pagination
	?>
<?PHP	//footer
include('php/footer.php');
?>